package com.cloudcc.mobile.adapter;

import java.lang.reflect.Field;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.ModuleMessage;
import com.cloudcc.mobile.util.Tools;
/**
 * 首页菜单适配器
 * @author Administrator
 *
 */
public class MenuAdapter extends BaseAdapter{
	private List<ModuleMessage> list;
	private Context context;
	private int[] userImage=new int[]{R.drawable.home_w,R.drawable.ic_social_cc_bcc,R.drawable.ic_location_place,R.drawable.ic_action_settings};
	public MenuAdapter(List<ModuleMessage> list, Context context) {
		super();
		this.list = list;
		this.context = context;
	}

	public void changeData(List<ModuleMessage> list){
		this.list=list;
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public ModuleMessage getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		try {
			if(position==0){
				convertView=LayoutInflater.from(context).inflate(R.layout.menu_fenge_cy, null);
			}else if(position==5){
				convertView=LayoutInflater.from(context).inflate(R.layout.menu_fenge_yy, null);
			}else if(position==list.size()-2){
				convertView=LayoutInflater.from(context).inflate(R.layout.menu_fenge_sz, null);
			}else{
				ViewHolder holder=new ViewHolder();
				convertView=LayoutInflater.from(context).inflate(R.layout.menu_list_item, null);
				holder.name=(TextView)convertView.findViewById(R.id.listname);
				holder.message=(TextView)convertView.findViewById(R.id.message_num);
				holder.img=(ImageView)convertView.findViewById(R.id.listimg);
				holder.name.setText(list.get(position).getName());
				holder.name.setTextSize(Tools.getFontSize(context));
				@SuppressWarnings("rawtypes")
				Class drawable=R.drawable.class;
				Field field = drawable.getField(list.get(position).getImage());
				int id = field.getInt(field.getName());
				holder.img.setImageBitmap(null);
				holder.img.setBackgroundResource(id);
				if(list.get(position).getNum()==0){
					holder.message.setVisibility(View.GONE);
				}else{
					holder.message.setVisibility(View.VISIBLE);
					holder.message.setText(""+list.get(position).getNum());
				}
			}
		} catch (Exception e) {
			Tools.handle(e);
		}
		return convertView;
	}
	class ViewHolder{
		private TextView name;
		private ImageView img;
		private TextView message ;
	}
}
